<?php

namespace App\Http\Controllers;

use App\Datatables;
use App\RunGameConfig;
use App\RunGameItems;
use App\RunGamePrize;
use App\RunGameReward;
use App\RunGameRole;
use App\RunGameSeason;
use Illuminate\Http\Request;

/**
 * Class RunGameController
 * @package App\Http\Controllers
 */
class RunGameController extends Controller
{
    /**
     * constructor.
     */
    public function __construct()
    {
        $this->middleware('auth');
    }


    /**
     * 题目列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function items(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(Datatables::RunGameItems());
        }

        return view('runGame.items');
    }

    /**
     * 导入题库
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
     */
    public function itemsAdd(Request $request)
    {

        return response()->json(RunGameItems::ItemsAdd($request));
    }

    /**
     * 赛季列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function seasons(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(Datatables::RunGameSeasons());
        }

        return view('runGame.seasons');
    }

    /**
     * 添加赛季
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function seasonsAdd(Request $request)
    {
        $start = strtotime($request->start);
        $end = strtotime($request->end);
        //赛季时间设置是否正确
        if (time() >= $start || $end <= $start) {
            return response()->json(['status' => 2, 'message' => '时间设置不正确']);
        }
        //是否有赛季重合
        if (RunGameSeason::where('end_at', '>=', $start)->exists()) {
            return response()->json(['status' => 2, 'message' => '赛季时间有重合']);
        }
        return response()->json(RunGameSeason::SeasonAdd($request));
    }

    /**
     * 删除赛季
     * @param RunGameSeason $runGameSeason
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function seasonsDel(RunGameSeason $runGameSeason)
    {
        if (!$runGameSeason->delete()) {
            return response()->json(['status' => 2, 'message' => '失败']);
        }
        return response()->json(['status' => 1, 'message' => '成功']);
    }

    /**
     * 赛季详情
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function seasonsInfo(Request $request, RunGameSeason $runGameSeason)
    {
        if ($request->isMethod('post')) {
            $start = strtotime($request->start);
            $end = strtotime($request->end);
            //赛季时间设置是否正确
            if (time() >= $start || $end <= $start) {
                return response()->json(['status' => 2, 'message' => '时间设置不正确']);
            }
            //是否有赛季重合
            if (RunGameSeason::where('end_at', '>=', $start)->exists()) {
                return response()->json(['status' => 2, 'message' => '赛季时间有重合']);
            }
            return response()->json(RunGameSeason::SeasonModify($runGameSeason, $request));
        }

        return view('runGame.seasonsInfo')->with(compact('runGameSeason'));
    }

    /**
     * 游戏奖品
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function reward(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(Datatables::SeasonsReward());
        }
        return view('runGame.reward');
    }

    /**
     * 添加奖品
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function rewardAdd(Request $request)
    {
        return response()->json(RunGameReward::RewardAdd($request));
    }

    /**
     * 删除奖品
     * @param RunGameReward $runGameReward
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function rewardDel(RunGameReward $runGameReward)
    {
        $runGameReward->start_at = 0;
        $runGameReward->is_use = RunGameReward::USE_NO;
        if (!$runGameReward->save()) {
            return response()->json(['status' => 2, 'message' => '失败']);
        }
        return response()->json(['status' => 1, 'message' => '成功']);
    }


    /**
     * 奖励列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function price(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(Datatables::RunGamePrice());
        }

        return view('runGame.price');
    }

    /**
     * 收货信息
     * @param RunGamePrize $runGamePrize
     * @return array
     */
    public function priceDetail(RunGamePrize $runGamePrize)
    {
        return response()->json(['name' => $runGamePrize->name, 'phone' => $runGamePrize->mobile, 'address' => $runGamePrize->province . $runGamePrize->city . $runGamePrize->county . $runGamePrize->address]);
    }

    /**
     * 发货
     * @param Request $request
     * @param RunGamePrize $runGamePrize
     * @return \Illuminate\Http\JsonResponse
     */
    public function priceSend(Request $request, RunGamePrize $runGamePrize)
    {
        if (!$request->has('express_name') || !$request->has('express_order')) {
            return response()->json(['status' => 2, 'message' => '缺少参数']);
        }

        return response()->json(RunGamePrize::PriceSend($runGamePrize, $request));
    }

    /**
     * 修改游戏配置
     * @param Request $request
     * @return $this|\Illuminate\Http\JsonResponse
     */
    public function config (Request $request)
    {
        $config = RunGameConfig::find(1);
        if ($request->isMethod('post')) {
            return response()->json(RunGameConfig::Modify($config, $request));
        }

        return view('runGame.config')->with(compact('config'));
    }

    /**
     * 游戏角色列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function role()
    {
        return response()->json(Datatables::RunGameRole());
    }

    /**
     * 添加角色
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function roleAdd(Request $request)
    {
        return response()->json(RunGameRole::AddRole($request));
    }

    /**
     * 删除角色
     * @param RunGameRole $runGameRole
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function roleDel(RunGameRole $runGameRole)
    {
        if (!$runGameRole->delete()) {
            return response()->json(['status' => 2, 'message' => '失败']);
        }
        return response()->json(['status' => 1, 'message' => '成功']);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function gameLog(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(Datatables::RunGameLog());
        }

        return view('runGame.gameLog');
    }

    /**
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function gameRank(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(Datatables::RunGameRank());
        }

        return view('runGame.gameRank');
    }
}
